
        
package com.mall.order.dao.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.ctg.mse.common.mysql.config.BaseMysqlConfig;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;


/**
 * @Description: Mysql静态配置，单/多数据源都可以基于此进行配置
 * 需要给一个数据源弄一个配置，注意scan的包需要区分不同数据源的包路径
 * @Author: zhouzhao
 * @CreateTime: 2022/10/19 10:38:29
 */
@Configuration
//该数据源对应的Mapperer类
@MapperScan(basePackages = MysqlConfig.PACKAGE,
        sqlSessionFactoryRef = "exampleSqlSessionFactory")
public class MysqlConfig extends BaseMysqlConfig {

    //需要扫描对应的包，因为这里是单数据源，dao包下不用区分数据源包
    //如果是多数据源，dao下还需要区分数据源包名，比如dao.example
    static final String PACKAGE = "com.mall.order.dao";

    @Override
    protected String getMapperLocation() {
        return "classpath:mybatis/**/*.xml";
    }

    @Override
    protected String getPackage() {
        return PACKAGE;
    }

    @Bean(name = "exampleDataSource")
    @ConfigurationProperties(prefix = "mysql.base.druid")//注入prefix开头的数据源用户名密码，JDBC URL
    public DataSource setDataSource() {
        return new DruidDataSource();
    }

    @Bean(name = "exampleTransactionManager")
    public DataSourceTransactionManager setTransactionManager(@Qualifier("exampleDataSource") DataSource dataSource) {
        return getTransactionManager(dataSource);
    }

    @Bean(name = "exampleSqlSessionFactory")
    public SqlSessionFactory setSqlSessionFactory(@Qualifier("exampleDataSource") DataSource dataSource) throws Exception {
        return getSqlSessionFactory(dataSource);
    }

    /**
     * 可以定制自己需要添加的拦截器，默认只有分页拦截器
     */
    @Override
    protected MybatisPlusInterceptor getInterceptor() {
        return super.getInterceptor();
    }
}